package com.ning.weixin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ning.weixin.dto.IndexDto;
import com.ning.weixin.entity.ClockAddress;
import com.ning.weixin.entity.UserAddress;
import com.ning.weixin.entity.UserInfo;
import com.ning.weixin.service.ClockAddressService;
import com.ning.weixin.service.UserAddressService;
import com.ning.weixin.service.UserInfoService;
import com.ning.weixin.utils.R;
import com.ning.weixin.utils.ScoreUtil;
import com.ning.weixin.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户打卡签到Controller
 *
 * @author Cyan
 * @Date:2018/12/11
 */
@Api(description = "打卡签到")
@RestController
@RequestMapping("/njxs/clock")
public class ClockController {

    private static Logger LOGGER = LoggerFactory.getLogger(ClockController.class);

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private ClockAddressService clockAddressService;

    @ApiOperation("线上打卡")
    @RequestMapping(value = "/upclock", method = RequestMethod.POST)
    public Map<String, Object> upClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null && userInfoService.findUserInfo(openId).getClockNum() > 0) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            Integer clockNum = userInfo.getClockNum();
            score = score + userInfo.getScore();
            map.put("score",score);
            //线上打卡次数减1
            userInfoService.subClockNum(new UserInfo(openId, clockNum));
            map.put("clockNum",userInfoService.findUserInfo(openId).getClockNum());
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            //保存
            userInfoService.updateScore(new UserInfo(openId, score, null, clockTime));
            return map;
        }
        return R.error("您的线上打卡次数不足，请先转发获取线上打卡次数");
    }

    @ApiOperation("圣诞树放肆红包雨")
    @RequestMapping(value = "SDclock", method = RequestMethod.POST)
    public Map<String, Object> SDClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            score = score + userInfo.getScore();
            map.put("score",score);
            //线下打卡次数减1
            //1.根据openId获取打卡地址id
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            String SDAddressId = addressIdList.get(0);
            Integer downClockNum = (Integer) userAddressService.findAddressById(openId).get(0).get("downClockNum");
            if (downClockNum > 0){
                userAddressService.subDownClockNum(new UserAddress(openId,SDAddressId,downClockNum));
            }
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            //保存
            userInfoService.updateScore(new UserInfo(openId, score,
                    clockAddressService.getClockAddress(SDAddressId), clockTime));
            return map;
        }
        return R.error("您的线下打卡次数不足，请先转发获取线下打卡次数");
    }

    @ApiOperation("新能源玩趣实验室")
    @RequestMapping(value = "WQclock", method = RequestMethod.POST)
    public Map<String, Object> WQClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            score = score + userInfo.getScore();
            map.put("score",score);
            //线下打卡次数减1
            //1.根据openId获取打卡地址id
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            String WQAddressId = addressIdList.get(1);
            Integer downClockNum = (Integer) userAddressService.findAddressById(openId).get(1).get("downClockNum");
            if (downClockNum > 0){
                userAddressService.subDownClockNum(new UserAddress(openId,WQAddressId,downClockNum));
            }
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            map.put("clockNum",userInfoService.findUserInfo(openId).getDownClockNum());
            //保存
            userInfoService.updateScore(new UserInfo(openId, score,
                    clockAddressService.getClockAddress(WQAddressId), clockTime));
            return map;
        }
        return R.error("您的线下打卡次数不足，请先转发获取线下打卡次数");
    }

    @ApiOperation("一元抢鲜研究所")
    @RequestMapping(value = "YYclock", method = RequestMethod.POST)
    public Map<String, Object> YYClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            score = score + userInfo.getScore();
            map.put("score",score);
            //线下打卡次数减1
            //1.根据openId获取打卡地址id
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            String YYAddressId = addressIdList.get(2);
            Integer downClockNum = (Integer) userAddressService.findAddressById(openId).get(2).get("downClockNum");
            if (downClockNum > 0){
                userAddressService.subDownClockNum(new UserAddress(openId,YYAddressId,downClockNum));
            }
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            map.put("clockNum",userInfoService.findUserInfo(openId).getDownClockNum());
            //保存
            userInfoService.updateScore(new UserInfo(openId, score,
                    clockAddressService.getClockAddress(YYAddressId), clockTime));
            return map;
        }
        return R.error("您的线下打卡次数不足，请先转发获取线下打卡次数");
    }

    @ApiOperation("玩钻派对")
    @RequestMapping(value = "WZclock", method = RequestMethod.POST)
    public Map<String, Object> WZClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            score = score + userInfo.getScore();
            map.put("score",score);
            //线下打卡次数减1
            //1.根据openId获取打卡地址id
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            String WZAddressId = addressIdList.get(3);
            Integer downClockNum = (Integer) userAddressService.findAddressById(openId).get(3).get("downClockNum");
            if (downClockNum > 0){
                userAddressService.subDownClockNum(new UserAddress(openId,WZAddressId,downClockNum));
            }
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            map.put("clockNum",userInfoService.findUserInfo(openId).getDownClockNum());
            //保存
            userInfoService.updateScore(new UserInfo(openId, score,
                    clockAddressService.getClockAddress(WZAddressId), clockTime));
            return map;
        }
        return R.error("您的线下打卡次数不足，请先转发获取线下打卡次数");
    }

    @ApiOperation("小米智能展")
    @RequestMapping(value = "XMclock", method = RequestMethod.POST)
    public Map<String, Object> XMClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            score = score + userInfo.getScore();
            map.put("score",score);
            //线下打卡次数减1
            //1.根据openId获取打卡地址id
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            String XMAddressId = addressIdList.get(4);
            Integer downClockNum = (Integer) userAddressService.findAddressById(openId).get(4).get("downClockNum");
            if (downClockNum > 0){
                userAddressService.subDownClockNum(new UserAddress(openId,XMAddressId,downClockNum));
            }
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            map.put("clockNum",userInfoService.findUserInfo(openId).getDownClockNum());
            //保存
            userInfoService.updateScore(new UserInfo(openId, score,
                    clockAddressService.getClockAddress(XMAddressId), clockTime));
            return map;
        }
        return R.error("您的线下打卡次数不足，请先转发获取线下打卡次数");
    }

    @ApiOperation("包骏新能源青秀万达体验店")
    @RequestMapping(value = "WDclock", method = RequestMethod.POST)
    public Map<String, Object> WDClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            score = score + userInfo.getScore();
            map.put("score",score);
            //线下打卡次数减1
            //1.根据openId获取打卡地址id
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            String WDAddressId = addressIdList.get(5);
            Integer downClockNum = (Integer) userAddressService.findAddressById(openId).get(5).get("downClockNum");
            if (downClockNum > 0){
                userAddressService.subDownClockNum(new UserAddress(openId,WDAddressId,downClockNum));
            }
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            map.put("clockNum",userInfoService.findUserInfo(openId).getDownClockNum());
            //保存
            userInfoService.updateScore(new UserInfo(openId, score,
                    clockAddressService.getClockAddress(WDAddressId), clockTime));
            return map;
        }
        return R.error("您的线下打卡次数不足，请先转发获取线下打卡次数");
    }

    @ApiOperation("宁家鲜生体验中心")
    @RequestMapping(value = "NJXSclock", method = RequestMethod.POST)
    public Map<String, Object> NJXSClock(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        Map<String,Object> map = new HashMap<>();
        //获取用户基本信息
        Integer score = ScoreUtil.scoreGet();
        if (score != null) {
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            score = score + userInfo.getScore();
            map.put("score",score);
            //线下打卡次数减1
            //1.根据openId获取打卡地址id
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            String NJXSAddressId = addressIdList.get(6);
            Integer downClockNum = (Integer) userAddressService.findAddressById(openId).get(6).get("downClockNum");
            if (downClockNum > 0){
                userAddressService.subDownClockNum(new UserAddress(openId,NJXSAddressId,downClockNum));
            }
            //获取打卡时间
            String clockTime = TimeUtil.getTime();
            map.put("clockNum",userInfoService.findUserInfo(openId).getDownClockNum());
            //保存
            userInfoService.updateScore(new UserInfo(openId, score,
                    clockAddressService.getClockAddress(NJXSAddressId), clockTime));
            return map;
        }
        return R.error("您的线下打卡次数不足，请先转发获取线下打卡次数");
    }

}
